﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WindowsForms
{
    public partial class FrmUserManage : Form
    {
        public FrmUserManage()
        {
            InitializeComponent();
            

                #region   初始化控件缩放

                x = Width;
                y = Height;
                setTag(this);

                #endregion
           
        }

        private void lstUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstUserList.SelectedIndex != -1)
            {
                User cm = (User)lstUserList.SelectedItem;
                txtPwd.Text = cm.Pwd;
                txtName.Text = Convert.ToString(cm.Username);
                cmbRole.Text = cm.Role;

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UserManage use1 = new UserManage();
            int clubid = int.Parse(use1.GetALLUsers("SELECT * FROM user, clubmanage WHERE user.userid = clubmanage.userid").Count.ToString())+1;
            string pwd = txtPwd.Text;
            int userid = int.Parse(use1.GetALLUsers("SELECT * FROM user, clubmanage WHERE user.userid = clubmanage.userid").Count.ToString())+1;

            string username = txtName.Text;
            string role = cmbRole.Text;

            User cm = new User(clubid, pwd, role, userid, username);
            lstUserList.Items.Add(cm);

            UserManage userbll = new UserManage();
            if (userbll.AddUsers(cm))
                MessageBox.Show("用户添加成功!");
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (lstUserList.SelectedIndex != -1)
            {
                lstUserList.Items.Remove(lstUserList.SelectedItem);
                lstUserList.SelectedIndex = 0;
            }
            int clubid = int.Parse(txtClubid.Text);
            string pwd = txtPwd.Text;
            int userid = int.Parse(txtUserid.Text);
            string username = txtName.Text;
            string role = cmbRole.Text;

            User cm = new User(clubid, pwd, role, userid, username);
            

            UserManage userbll = new UserManage();
            if (userbll.DelUsers(cm))
                MessageBox.Show("用户删除成功!");
        }

        private void FrmUserManage2_Load(object sender, EventArgs e)
        {
            UserManage  userbll= new UserManage();
            dgvUser.AutoGenerateColumns = false;
            string sql = "SELECT * FROM user, clubmanage WHERE user.userid = clubmanage.userid";
            dgvUser.DataSource = userbll.GetALLUsers(sql);

            User user1 = new User(1, "123456", "管理员", 123456, "123");
            User user2 = new User(2, "123456", "用户", 2, "张三");
            User user3 = new User(3, "123456", "用户", 3, "李四");
            User user4 = new User(4, "123456", "用户", 4, "王五");
            User user5 = new User(5, "123456", "管理员", 5, "糖6");


            lstUserList.Items.Add(user1);
            lstUserList.Items.Add(user2);
            lstUserList.Items.Add(user3);
            lstUserList.Items.Add(user4);
            lstUserList.Items.Add(user5);
            lstUserList.DisplayMember = "username";

            User cm = (User)dgvUser.CurrentRow.DataBoundItem;
            LoadDetail(cm);

        }

        private void LoadDetail(User cm)
        {
            txtClubid.Text = cm.Clubid.ToString();
            txtPwd.Text = cm.Pwd.ToString();
            txtName.Text=cm.Username.ToString();
            txtUserid.Text=cm.Userid.ToString();
            cmbRole.Text = cm.Role.ToString();
            /*int clubid = int.Parse(txtClubid.Text);
            string pwd = txtPwd.Text;
            int userid = int.Parse(txtUserid.Text);
            string username = txtName.Text;
            string role = cmbRole.Text;*/
        }

        private void btnShowPwd_Click(object sender, EventArgs e)
        {
            this.txtPwd.PasswordChar = '\0';
        }

        private void dgvUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            User cm = (User)dgvUser.CurrentRow.DataBoundItem;
            LoadDetail(cm);
        }

        private void FrmUserManage_Resize(object sender, EventArgs e)
        {
                //重置窗口布局
                ReWinformLayout();
        }

        #region 控件大小随窗体大小等比例缩放

        private readonly float x; //定义当前窗体的宽度
        private readonly float y; //定义当前窗体的高度

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0) setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    var mytag = con.Tag.ToString().Split(';');
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
                    var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小                   
                    if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    con.Focus();
                    if (con.Controls.Count > 0) setControls(newx, newy, con);
                }
        }


        /// <summary>
        /// 重置窗体布局
        /// </summary>
        private void ReWinformLayout()
        {
            var newx = Width / x;
            var newy = Height / y;
            setControls(newx, newy, this);

        }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            UserManage use1 = new UserManage();

            
            int clubid = int.Parse(use1.GetALLUsers("SELECT * FROM user, clubmanage WHERE user.userid = clubmanage.userid").Count.ToString())+1;
            string pwd = txtPwd.Text;
            int userid = int.Parse(use1.GetALLUsers("SELECT * FROM user, clubmanage WHERE user.userid = clubmanage.userid").Count.ToString())+1;
            string username = txtName.Text;
            string role = cmbRole.Text;

            User cm = new User(clubid, pwd, role, userid, username);
            lstUserList.Items.Add(cm);

            UserManage userbll = new UserManage();
            if (userbll.AddUsers(cm))
                MessageBox.Show("用户保存成功!");
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int clubid = int.Parse(txtClubid.Text);
            string pwd = txtPwd.Text;
            int userid = int.Parse(txtUserid.Text);
            string username = txtName.Text;
            string role = cmbRole.Text;

            User cm = new User(clubid, pwd, role, userid, username);
            

            UserManage userbll = new UserManage();
            if (userbll.UptUsers(cm))
                MessageBox.Show("用户修改成功!");
        }
    }
}
